// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:desktop_webview_window/desktop_webview_window.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/super/FFInk.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:mybase/base/tool/extension/FFListExt.dart';
import 'package:webview_window_example/pages/englishChinese/EnglishChinesePageStateEvent.dart';
import 'package:webview_window_example/utils/navi/AppNavi.dart';
import 'package:webview_window_example/entity/TranslateEntity.dart';
import 'package:webview_window_example/textField/FFMoreTextField.dart';

import 'dart:async';

import 'package:mybase/base/tool/empty/FFEmpty.dart';

import 'package:flutter_test/flutter_test.dart';

class EnglishChinesePage extends FFScaffold {
  String? name;

  EnglishChinesePage({
    Key? key,
    this.name,
  }) : super(key: key);

  @override
  EnglishChinesePageState createState() => EnglishChinesePageState();
}

class EnglishChinesePageState extends FFScaffoldState
    with MFFScaffold
    implements IWebviewWindow {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  EnglishChinesePage get widget => super.widget as EnglishChinesePage;

  @override
  IFFRefresh? get iffRefresh => refresh;
  late EnglishChinesePageRefresh refresh =
      EnglishChinesePageRefresh(state: this);

  /**固定写法end */

  final TextEditingController textController = TextEditingController();
  final TextEditingController resultController = TextEditingController();

  List<TranslateEntity> entitys = [
    TranslateEntity(code: "zh-Hans", title: "简体", urlCode: "zh-CN"),
    TranslateEntity(code: "en", title: "英文", urlCode: "en"),
  ];

  int selectIndex = 0;

  bool? webviewAvailable;
  late Webview webview;

  String? targetDir;

  @override
  void ffInitState() {
    super.ffInitState();
    WebviewWindow.isWebviewAvailable().then((value) {
      setState(() {
        webviewAvailable = value;
      });
    });
  }

  @override
  void ffDispose() {
    textController.dispose();
    super.ffDispose();
  }

  @override
  Widget? ffNavi(BuildContext context) {
    return AppNavi.createNavi(
      context,
      title: "flutter项目内翻译",
    );
  }

  @override
  Widget? ffBody(BuildContext context) {
    List<Widget> slivers = [
      SliverToBoxAdapter(
        child: Container(
          child: FFLineContainer(
            children: [
              FFLineContainer(
                direction: Axis.horizontal,
                children: [
                  FFInk(
                    child: FFTextButton(
                      margin: EdgeInsets.only(top: 8),
                      unconstrainedBox: true,
                      decoration: BoxDecoration(
                        color: Colors.red,
                      ),
                      text: "点击翻译成${entitys[1].title}",
                      fontSize: 16,
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16, vertical: 8),
                      onClick: () {
                        selectIndex = 1;
                        onStart();
                      },
                    ),
                  ),
                  SizedBox(
                    width: 16,
                  ),
                  FFInk(
                    child: FFTextButton(
                      margin: EdgeInsets.only(top: 8),
                      unconstrainedBox: true,
                      decoration: BoxDecoration(
                        color: Colors.red,
                      ),
                      text: "点击翻译成${entitys[0].title}",
                      fontSize: 16,
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16, vertical: 8),
                      onClick: () {
                        selectIndex = 0;
                        onStart();
                      },
                    ),
                  ),
                  SizedBox(
                    width: 16,
                  ),
                  FFInk(
                    child: FFTextButton(
                      margin: EdgeInsets.only(top: 8),
                      unconstrainedBox: true,
                      decoration: BoxDecoration(
                        color: Colors.red,
                      ),
                      text: "交换内容",
                      fontSize: 16,
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16, vertical: 8),
                      onClick: () {
                        if (FFEmpty.stringIsEmpty(resultController.text)) {
                          SmartDialog.showToast("结果为空,不用交换",
                              displayTime: Duration(milliseconds: 1000));
                          return;
                        }

                        textController.text = resultController.text;
                        resultController.text = "";
                        setState(() {});
                      },
                    ),
                  ),
                ],
              ),
              Container(
                color: Colors.pink,
                padding: const EdgeInsets.all(16),
                margin: EdgeInsets.only(top: 12),
                child: FFMoreTextField(
                  controller: textController,
                  onValid: (bool valid, String value) {},
                  min: 0,
                  max: 999999,
                  height: 100,
                  placeStr: "这儿是待翻译的内容",
                  placeTextStyle: const TextStyle(color: Colors.grey),
                ),
              ),
              Container(
                color: Colors.pink,
                padding: const EdgeInsets.all(16),
                margin: EdgeInsets.only(top: 12),
                child: FFMoreTextField(
                  controller: resultController,
                  onValid: (bool valid, String value) {},
                  min: 0,
                  max: 999999,
                  height: 100,
                  placeStr: "这儿是结果",
                  placeTextStyle: const TextStyle(color: Colors.grey),
                ),
              ),
              FFInk(
                child: FFTextButton(
                  unconstrainedBox: true,
                  text: "复制结果",
                  decoration: BoxDecoration(
                    color: Colors.green,
                  ),
                  onClick: () {
                    if (FFEmpty.stringIsEmpty(resultController.text)) {
                      SmartDialog.showToast("结果为空",
                          displayTime: Duration(milliseconds: 1000));
                      return;
                    }
                    Clipboard.setData(
                        ClipboardData(text: resultController.text));
                  },
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                  margin: EdgeInsets.only(top: 16),
                ),
              ),
            ],
          ),
        ),
      ),
    ];
    return CustomScrollView(slivers: slivers);
  }

  ///通知页面加载完成，可以提取翻译了
  @override
  didFinished(int viewId) async {
    await Future.delayed(const Duration(milliseconds: 350));
    extractTranslate();
  }
}

///刷新组件
class EnglishChinesePageRefresh with MFFRefresh {
  EnglishChinesePageState state;

  EnglishChinesePageRefresh({required this.state});

  @override
  bool? ffCanRefreshHead() {
    return true;
  }

  @override
  ffRefresh(bool isHead) {
    ffRefreshResult(isHead, test, state);
  }

  //具体请求业务
  Future<IFFR> test(IRefreshParam? iRefreshParam) async {
    List? list;
    await Future.wait([
      //HomeApi.index_recentlyPlayed(),
    ]).then((value) {
      list = value;
    });

    IFFR? r = list?.safeValue(0);
    return r ?? R(rStatus: RStatus.result);
  }
}
